package com.lyz.mapDemo;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @ClassName MapDemo2
 * @Description 统计任意学生，春游地点的选择结果
 * @Author 2214361854
 * @Date 2022/2/22 21:48
 * @Version 1.0
 */
public class MapDemo2 {
    public static void main(String[] args) {
        //80个学生
        int stuend = 80;
        //选择范围：ABCD "西湖","长城","华山","泰山"
        String[] selects = {"A","B","C","D"};
        //统计全部选择
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < stuend; i++) {
            //生成随机数（范围：selects长度）添加到StringBuilder
            sb.append(selects[r.nextInt(selects.length)]);
        }
        System.out.println(sb);

        //统计每个选择结果数量
        Map<Character, Integer> infos = new HashMap<>();

        //遍历全部选择并统计每种结果数量
        for (int i = 0; i < sb.length(); i++) {
            char ch = sb.charAt(i);
            //键存在原值加1
            if (infos.containsKey(ch)) {
                infos.put(ch,infos.get(ch)+1);
            } else {
                infos.put(ch,1);
            }
        }
        System.out.println(infos);
    }
}
